package v1

import (
	"github.com/gin-gonic/gin"
	"github.com/spf13/cast"
	"strconv"
	"tdy-backend/app/core"
	memberService "tdy-backend/app/member/service"
	"tdy-backend/app/panoramic/dto"
	"tdy-backend/app/panoramic/service"
	"tdy-backend/global"
	"tdy-backend/utils"
	"time"
)

type MaterialController struct {
}

// @Summary 素材提交
// @Tags 管理端-全景素材
// @Param       token	header string true "登录标识"
// @Param       body	body dto.MaterialCommitSubDto true "提交参数"
// @Param       Upload	body string true "批量文件"
// @Router /Material/Commit [post]
func (o *MaterialController) Commit(c *gin.Context) {
	var err error
	var param dto.MaterialCommitDto
	if err = c.ShouldBind(&param); err != nil {
		//global.TYD_LOGGER.Error(err.Error())
		core.CustomError(c, "参数缺失")
		return
	}
	form, _ := c.MultipartForm()
	files := form.File["Upload"]

	subParam := []dto.MaterialCommitSubDto{}
	uploadPath := global.TYD_CONFIG.GetString("file.uploadPath")
	limitSize := global.TYD_CONFIG.GetInt64("file.limitSize")

	for _, file := range files {
		if file.Size > limitSize*1024*1024 {
			core.CustomError(c, file.Filename+"，此文件超出限制大小")
			return
		}
		mimeType := utils.IsSupport(file.Header["Content-Type"][0])
		if !mimeType {
			core.CustomError(c, file.Filename+",此上传的文件类型不支持")
			return
		}
		unique := utils.CreateUniqueKey()
		path := uploadPath + cast.ToString(time.Now().Year()) + "/" + cast.ToString(int(time.Now().Month())) + "/" + cast.ToString(time.Now().Day()) + "/" + unique + "/"
		utils.CreateDir(path + "/panoramic")
		filename := path + unique + "." + utils.GetFileExt(file.Filename)
		subParam = append(subParam, dto.MaterialCommitSubDto{
			Title:     file.Filename,
			Panoramic: filename,
			Size:      file.Size,
		})
		// 上传文件至指定目录
		c.SaveUploadedFile(file, "./"+filename)
	}
	db := global.TYD_DB
	token := c.Request.Header.Get("token")
	memberService := memberService.MemberService{}
	memberService.MakeORM(db)
	memberInfo, err := memberService.GetCurrentMemberInfo(token)
	if err != nil {
		core.CustomError(c, err.Error())
		return
	}

	materialService := service.MaterialService{}
	param.SubParam = &subParam
	materialService.MakeORM(db).MakeMemberInfo(memberInfo)
	err = materialService.Add(&param)
	if err != nil {
		core.CustomError(c, err.Error())
		return
	}
	core.Ok(c, nil, "上传成功")
}

// @Summary 素材编辑
// @Tags 管理端-全景素材
// @Param       token	header string true "登录标识"
// @Param	body	body	dto.MaterialEditDto true "编辑全景素材参数"
// @Router /Material/Edit [post]
func (o *MaterialController) Edit(c *gin.Context) {
	var err error
	var param dto.MaterialEditDto

	if err = c.ShouldBindJSON(&param); err != nil {
		core.CustomError(c, err.Error())
		return
	}
	db := global.TYD_DB
	token := c.Request.Header.Get("token")
	memberService := memberService.MemberService{}
	memberService.MakeORM(db)
	memberInfo, err := memberService.GetCurrentMemberInfo(token)
	if err != nil {
		core.CustomError(c, err.Error())
		return
	}
	materialService := service.MaterialService{}

	materialService.MakeORM(db).MakeMemberInfo(memberInfo)
	err = materialService.Edit(&param)
	if err != nil {
		core.CustomError(c, err.Error())
		return
	}
	core.Ok(c, nil, "编辑成功")
}

// @Summary 素材批量删除
// @Tags 管理端-全景素材
// @Param       token	header string true "登录标识"
// @Param	body	body	dto.MaterialDeleteDto true "批量删除全景作品参数"
// @Router /Material/Delete [post]
func (o *MaterialController) Delete(c *gin.Context) {
	var err error
	var param dto.MaterialDeleteDto

	if err = c.ShouldBindJSON(&param); err != nil {
		core.CustomError(c, err.Error())
		return
	}
	db := global.TYD_DB
	token := c.Request.Header.Get("token")
	memberService := memberService.MemberService{}
	memberService.MakeORM(db)
	memberInfo, err := memberService.GetCurrentMemberInfo(token)
	if err != nil {
		core.CustomError(c, err.Error())
		return
	}

	materialService := service.MaterialService{}
	materialService.MakeORM(db).MakeMemberInfo(memberInfo)
	err = materialService.Delete(&param)
	if err != nil {
		core.CustomError(c, err.Error())
		return
	}
	core.Ok(c, nil, "删除成功")
}

// @Summary 素材分页查询
// @Tags 管理端-全景素材
// @Param       token	header string true "登录标识"
// @Param	Page	query	uint false "页码"
// @Param	PageSize	query	uint false "页尺寸"
// @Param	Keyword	query	string false "搜索关键词"
// @Param	Kind	query	string true "类型 1普清  2高清"
// @Router /Material/ListForPage [get]
func (o *MaterialController) ListForPage(c *gin.Context) {
	var err error
	var param dto.MaterialListDto

	if err = c.Bind(&param); err != nil {
		core.CustomError(c, "参数缺失")
		return
	}
	db := global.TYD_DB
	token := c.Request.Header.Get("token")
	memberService := memberService.MemberService{}
	memberService.MakeORM(db)
	memberInfo, err := memberService.GetCurrentMemberInfo(token)
	if err != nil {
		core.CustomError(c, err.Error())
		return
	}
	msr := service.MaterialService{}

	msr.MakeORM(db).MakeMemberInfo(memberInfo)
	data := msr.ListForPage(&param)
	core.Ok(c, data, "请求成功")
}

// @Summary 素材详情
// @Tags 管理端-全景素材
// @Param       token	header string true "登录标识"
// @Param	MaterialId	query	uint true "素材id"
// @Router /Material/Detail [get]
func (o MaterialController) Detail(c *gin.Context) {
	materialId := c.DefaultQuery("MaterialId", "0")
	id, _ := strconv.ParseUint(materialId, 10, 64)
	token := c.Request.Header.Get("token")
	db := global.TYD_DB
	memberService := memberService.MemberService{}
	memberService.MakeORM(db)
	memberInfo, err := memberService.GetCurrentMemberInfo(token)
	if err != nil {
		core.CustomError(c, err.Error())
		return
	}
	materialService := service.MaterialService{}
	materialService.MakeORM(db).MakeMemberInfo(memberInfo)
	row, err := materialService.GetMaterialOneRow(id)
	if err != nil {
		core.CustomError(c, err.Error())
		return
	}
	core.Ok(c, row, "请求成功")
}
